package com.ermace.robo.jumper.game.GameObjects;

import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;

import com.ermace.robo.jumper.game.Line;
import com.ermace.robo.jumper.game.Player.Position;
import com.ermace.robo.jumper.game.Player.Robot;

public class Rectangle extends GameObject {
	
	/**
    *
    * This field is needed for Android to be able to
    * create new objects, individually or as arrays.
    *
    * This also means that you can use use the default
    * constructor to create the object and use another
    * method to hyrdate it as necessary.
    *
    */
	public static final Parcelable.Creator<Rectangle> CREATOR =
			new Parcelable.Creator<Rectangle>() {
		@Override
		public Rectangle createFromParcel(Parcel in) {
			return new Rectangle(in);
		}

		@Override
		public Rectangle[] newArray(int size) {
			return new Rectangle[size];
		}
	};
	
	public Rectangle(double x, double y,double length, int pic) {
		super(x, y, length,  pic);
	}
	
	public Rectangle(Parcel in) {
		super(in);
	}
	
	@Override
	public Rect getRect() {
		double x = getPxPosition().getX();
		double y = getPxPosition().getY();
				
		return new Rect((int)x, (int)y,
				(int)(x + getPxLength()),(int) (y + getPxHeight()));
	}
	
	@Override
	public boolean contains(Position pos) {
		double xPos = pos.getX();
		double yPos = pos.getY();
		
		if ((xPos > getPxPosition().getX() + getPxLength() && xPos > getPxPosition().getX()) 
				|| (xPos < getPxPosition().getX() + getPxLength() && xPos < getPxPosition().getX()))
			return false; // x-coordinate is not in the boundaries of the rectangle
		if ((yPos > getPxPosition().getY() + getPxHeight() && yPos > getPxPosition().getY()) 
				|| (yPos < getPxPosition().getY() + getPxHeight() && yPos < getPxPosition().getY()))
			return false; // y-coordinate is not in the boundaries of the rectangle
		
		return true;
	}
	
//	@Override
//	public boolean collide(double x, double y) {
//		if (y + Robot.RADIUS_VERTICAL >= (double)(super.position.getY() - height/2) && y + Robot.RADIUS_VERTICAL <= (double) (super.position.getY() + height/2) && x >= (double)(super.position.getX() - length/2) && x <= (double) (super.position.getX() + height/2))
//		{
//			return true;
//		}
//		return false;
//	}
	
	@Override
	public Position collide(Line line) {
		Double x = line.getXCorrespondingTo(getPxPosition().getY());
		if (x.equals(Double.NaN))
			return null;
		
		Position result = new Position(x, getPxPosition().getY());
		if (contains(result))
			return result;
		
		return null;
	}
	
	@Override
	public void interact(Robot robot) {
		return;			// Does nothing
	}
	@Override
	public void update(double TimeIntervalMS) {
		return;			// Does nothing
	}

//	@Override		TODO mag wrs weg
//	public void draw(Canvas canvas, Resources res) {
//		
//		canvas.drawBitmap(Bitmaps.getInstance(res).getBitmap(getPictureId()),
//				null, getRect(), null);
//		
//	}
}
